package com.yic.module.wms.service.receiveapply;

import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import com.yic.module.wms.controller.admin.receiveapply.vo.*;
import com.yic.module.wms.dal.dataobject.receiveapply.ReceiveApplyDO;
import com.yic.framework.common.pojo.PageResult;

import com.yic.module.wms.convert.receiveapply.ReceiveApplyConvert;
import com.yic.module.wms.dal.mysql.receiveapply.ReceiveApplyMapper;

import static com.yic.framework.common.exception.util.ServiceExceptionUtil.exception;
import static com.yic.module.wms.enums.ErrorCodeConstants.*;

/**
 * 领料申请单 Service 实现类
 *
 * @author 系统管理员
 */
@Service
@Validated
public class ReceiveApplyServiceImpl implements ReceiveApplyService {

    @Resource
    private ReceiveApplyMapper receiveApplyMapper;

    @Override
    public Long createReceiveApply(ReceiveApplyCreateReqVO createReqVO) {
        // 插入
        ReceiveApplyDO receiveApply = ReceiveApplyConvert.INSTANCE.convert(createReqVO);
        receiveApplyMapper.insert(receiveApply);
        // 返回
        return receiveApply.getId();
    }

    @Override
    public void updateReceiveApply(ReceiveApplyUpdateReqVO updateReqVO) {
        // 校验存在
        validateReceiveApplyExists(updateReqVO.getId());
        // 更新
        ReceiveApplyDO updateObj = ReceiveApplyConvert.INSTANCE.convert(updateReqVO);
        receiveApplyMapper.updateById(updateObj);
    }

    @Override
    public void deleteReceiveApply(Long id) {
        // 校验存在
        validateReceiveApplyExists(id);
        // 删除
        receiveApplyMapper.deleteById(id);
    }

    private void validateReceiveApplyExists(Long id) {
        if (receiveApplyMapper.selectById(id) == null) {
            throw exception(RECEIVE_APPLY_NOT_EXISTS);
        }
    }

    @Override
    public ReceiveApplyDO getReceiveApply(Long id) {
        return receiveApplyMapper.selectById(id);
    }

    @Override
    public List<ReceiveApplyDO> getReceiveApplyList(Collection<Long> ids) {
        return receiveApplyMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ReceiveApplyDO> getReceiveApplyPage(ReceiveApplyPageReqVO pageReqVO) {
        return receiveApplyMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ReceiveApplyDO> getReceiveApplyList(ReceiveApplyExportReqVO exportReqVO) {
        return receiveApplyMapper.selectList(exportReqVO);
    }

}
